package com.karry.yl.web.services;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.elder.Elderly;
import com.karry.yl.domain.healthdata.HealthDataItem;
import com.karry.yl.domain.services.GoOut;
import com.karry.yl.domain.services.Items;
import com.karry.yl.domain.services.Meal;
import com.karry.yl.domain.services.MealItems;
import com.karry.yl.service.elder.ElderlyService;
import com.karry.yl.service.healthdata.HealthDataItemService;
import com.karry.yl.service.services.GoOutService;
import com.karry.yl.service.services.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Administrator
 */
@Controller
@RequestMapping("/goOut")
public class GoOutController extends SuperController {


    @Autowired
    private GoOutService goOutService;
    @Autowired
    private ElderlyService elderlyService;


    @RequestMapping
    public String listUI() {
        return "services/goout/list";
    }


    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit) {
        Page<GoOut> mealPage = goOutService.selectPage(new Page<GoOut>(page, limit), new EntityWrapper<GoOut>().eq("orgId", currentUser.getOrgId()).orderBy("createdTime", false));
        return new LayTableResponse(mealPage.getRecords(), mealPage.getTotal());
    }


    @RequestMapping("/add")
    public String addUI() {
        return "services/goout/add";
    }

    /**
     * 选择老人页面
     *
     * @return
     */
    @RequestMapping("/select/elderly")
    public String selectElderlyUI() {
        return "services/goout/selectElderly";
    }

    /**
     * 查找所有老人 弹窗
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/select/elderly/list")
    @ResponseBody
    public LayTableResponse selectElderlyList(int page, int limit, String name) {
        Wrapper<Elderly> elderlyWrapper = new EntityWrapper<Elderly>().eq("orgId", currentUser.getOrgId());
        if (!StringUtils.isEmpty(name)) {
            elderlyWrapper.like("name", name);
        }
        Page<Elderly> elderlyPage = elderlyService.selectPage(new Page<Elderly>(page, limit), elderlyWrapper);
        return new LayTableResponse(elderlyPage.getRecords(), elderlyPage.getTotal());
    }

    /**
     * 保存外出记录
     *
     * @param goOut
     * @return
     */
    @RequestMapping("/save")
    public String save(GoOut goOut) {
        goOut.setStatus(0);
        goOut.setOrgId(currentUser.getOrgId());
        goOut.setResponsibilityPerson(currentUser.getUserName());
        goOut.setResponsibilityPersonId(currentUser.getId());
        goOutService.insert(goOut);
        return "redirect:/goOut";
    }

    /**
     * 更改外出的状态
     *
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResponse updateStatus(Long id, Integer status) {
        GoOut goOut = goOutService.selectById(id);
        goOut.setStatus(status);
        goOutService.updateById(goOut);
        return  JsonResponse.success();
    }
}
